package com.yangmf.auto_message_handle.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yangmf.auto_message_handle.constant.Message_Type;
import com.yangmf.auto_message_handle.constant.RedisConstant;
import com.yangmf.auto_message_handle.content.BaseContext;
import com.yangmf.auto_message_handle.mapper.DataMapper;
import com.yangmf.auto_message_handle.mapper.EventMapper;
import com.yangmf.auto_message_handle.pojo.dto.EventqueryDTO;
import com.yangmf.auto_message_handle.pojo.entity.Event;
import com.yangmf.auto_message_handle.pojo.entity.Mesg;
import com.yangmf.auto_message_handle.pojo.vo.EventVO;
import com.yangmf.auto_message_handle.result.PageResult;
import com.yangmf.auto_message_handle.service.EventService;
import com.yangmf.auto_message_handle.utils.AliOssUtil;
import com.yangmf.auto_message_handle.utils.ContentHighlightUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EventServiceImpl implements EventService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private ContentHighlightUtil contentHighlightUtil;
    @Autowired
    AliOssUtil aliOssUtil;

    @Override
    @Transactional
    public void addEvent(Event event) {


        EventVO eventVO = new EventVO();
        BeanUtils.copyProperties(event, eventVO);
        eventMapper.insertEvent(eventVO);
        log.info("添加基础事件");

        //添加文本信息
        log.info("添加事件文本信息");
        if (event.getEventContent().size() > 0) for (Mesg mesg : event.getEventContent()) {
            mesg.setEventId(eventVO.getId());
            dataMapper.insert(mesg);
        }
        //添加图片信息
        log.info("添加事件图片信息");
        if (event.getEventPic().size() > 0) for (Mesg mesg : event.getEventPic()) {
            mesg.setEventId(eventVO.getId());
            dataMapper.insert(mesg);
        }
        //添加文档信息
        log.info("添加事件文档信息");
        if (event.getEventDoc().size() > 0) for (Mesg mesg : event.getEventDoc()) {
            mesg.setEventId(eventVO.getId());
            dataMapper.insert(mesg);
        }
        //添加文件信息

        log.info("添加事件文件信息");

        if (event.getEventFile().size() > 0) for (Mesg mesg : event.getEventFile()) {
            mesg.setEventId(eventVO.getId());
            dataMapper.insert(mesg);
        }


        //实现向redis数据库的添加该事件包含的消息的 id
        log.info("添加事件包含的消息的id");
        for (Integer message_id : event.getEvent_message_id()) {
            // 添加元素到Set中
            stringRedisTemplate.opsForSet().add(RedisConstant.EVENT_START + event.getEventUuid(), message_id.toString());

// 设置过期时间为5天


        }
        stringRedisTemplate.expire(RedisConstant.EVENT_START + event.getEventUuid(), java.time.Duration.ofDays(5));
    }

    @Override
    public List<String> getEventuuid(LocalDateTime start_time) {


        List<String> eventuuid = eventMapper.getEventuuid(start_time);
        if (eventuuid.size() > 0) return eventuuid;

        return null;

    }

    @Override
    @Transactional
    public void addmessageToEvent(String event_uuid, String message) {

        //查询id从uuid
        Event event = eventMapper.getEventIdByUuid(event_uuid);

        Mesg mes = Mesg.builder().content(message).isDelete(0).createTime(LocalDateTime.now()).messageType(Message_Type.TEXT_MESSAGE).eventId(event.getId()).eventType(event.getEventType()).build();

        dataMapper.insert(mes);
    }

    ;

    @Override
    @Transactional
    public PageResult list(EventqueryDTO eventqueryDTO) {
        log.info("获取事件列表");
        PageHelper.startPage(eventqueryDTO.getPageNum(), eventqueryDTO.getPageSize());
        Page<EventVO> event2 = eventMapper.getEvent(eventqueryDTO);
        List<EventVO> event = event2.getResult();
        for (EventVO eventVO : event) {
            eventVO.setEventName(contentHighlightUtil.highlightContent(eventVO.getEventName(), eventqueryDTO.getEventName(), 10, 10));
        }
        long total = event2.getTotal();

        //判断是不是已读
        if (BaseContext.getCurrentId() != null) for (EventVO eventVO : event) {
            if (stringRedisTemplate.opsForSet().isMember(RedisConstant.EVENT_READ + eventVO.getEventUuid(), BaseContext.getCurrentId().toString())) {
                eventVO.setIsRead(1);
            } else {
                eventVO.setIsRead(0);
            }
        }


        log.info("获取事件列表成功");
        return new PageResult(total, event);


    }

    @Override
    @Transactional

    public Event getEventById(Long id) {
        Event event = eventMapper.getEventById(id);


        //1.获取文本消息
        List<Mesg> textMessages = dataMapper.getmessage(id, Message_Type.TEXT_MESSAGE);

        //2.获取图片消息
        List<Mesg> picMessages = dataMapper.getmessage(id, Message_Type.IMAGE_MESSAGE);

        //3.获取文档消息
        List<Mesg> docMessages = dataMapper.getmessage(id, Message_Type.TENCENT_DOC_MESSAGE);

        //4.获取文件消息
        List<Mesg> fileMessages = dataMapper.getmessage(id, Message_Type.FILE_MESSAGE);
        event.setEventContent(textMessages);
        event.setEventPic(picMessages);
        event.setEventDoc(docMessages);
        event.setEventFile(fileMessages);
        Long currentId = BaseContext.getCurrentId();
        if (currentId != null)
            stringRedisTemplate.opsForSet().add(RedisConstant.EVENT_READ + event.getEventUuid(), currentId.toString());


        return event;
    }


    @Override
    @Transactional
    public void batchDeleteEvents(List<Long> ids) {

        eventMapper.batchDelete(ids);
        List<Mesg> mesgs = dataMapper.selectFilesByEventId(ids);
        dataMapper.batchdeleteMessagesByEventId(ids);
        for(Mesg mesg:mesgs)
            aliOssUtil.deleteFile(mesg.getContent());


    }

    @Override
    public void mergeEvent(Long eventId1, Long eventId2) {
        eventMapper.batchDelete(List.of(eventId1));
        dataMapper.updateEventId(eventId1, eventId2);

    }

    ;

    @Override
    @Transactional
    public void archiveEvent(Long eventId, Long archiveId) {
        eventMapper.updateEventType(eventId, archiveId);
        dataMapper.updateEventTypeByEventType(eventId, archiveId);

    }

    ;


}
